﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Configuration.Assemblies;
using System.Configuration;
using System.Resources;
using System.Collections;

namespace UTLLOGER
{
    public class UTLLOGER
    {
        private string strFolder = ConfigurationManager.AppSettings["PathLogDefault"].ToString();        
        private string strFilename;
        private string strLocation;

        public UTLLOGER()
        {
            strFolder = ConfigurationManager.AppSettings["PathLogDefault"].ToString();
            strFilename = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year + ".log";
            strLocation = Application.StartupPath;
        }

        /// <summary>
        /// Ghi noi dung loi ra file text
        /// </summary>
        /// <param param name="IDlog">Ma Loi</param>
        /// <param param name="ParaLog">Tham so di vao; Neu khong co truyen vao la NULL</param> 
        private void WriteLog(string IDlog, string[] ParaLog)
        {
            string strParaLog = "";
            if (!Directory.Exists(strLocation + @"\" + strFolder))
            {
                Directory.CreateDirectory(strLocation + @"\" + strFolder);
            }
            FileStream file = new FileStream(strLocation + @"\" + strFolder + @"\" + strFilename, FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(file);
            if (ParaLog != null)
            {
                for (int i = 0; i < ParaLog.Length; i++)
                {
                    strParaLog += ParaLog[i].ToString();
                }
            }
            sw.WriteLine(ReadLogInfo(IDlog) + " | " + strParaLog);
            sw.Close();
            file.Close();
        }
        /// <summary>
        /// Doc noi dung cua log tu file CSV
        /// </summary>
        /// <param param name="IDlog">Ma Loi</param>        
        private string ReadLogInfo(string IDlog)
        {
            string _strLog = "";
            switch (IDlog)
            {
                case "LOG001":
                    _strLog = Properties.Resources.LOG001;
                    break;
                default:
                    _strLog = "Không Xác Định";
                    break;
            }
            
            return _strLog;
        }
        /// <summary>
        /// Xoa cac file log trong thu muc Log cua ung dung
        /// </summary> 
        private void DeleteLog()
        {
            if (Directory.Exists(strLocation + @"\" + strFolder))
            {
                DirectoryInfo _DirectoryInfo = new DirectoryInfo(strLocation + @"\" + strFolder);
                foreach (FileInfo _FileInfo in _DirectoryInfo.GetFiles("*.*"))
                {
                    _FileInfo.Delete();
                }
            }
        }
        /// <summary>
        /// Xoa mot file log tuy y trong thu muc Log cua ung dung
        /// </summary> 
        private void DeleteLog(string strFileName)
        {
            if (File.Exists(strLocation + @"\" + strFolder + @"\" + strFileName))
            {
                File.Delete(strLocation + @"\" + strFolder + @"\" + strFileName);
            }
        }
        /// <summary>
        /// Ghi noi dung loi ra file text
        /// </summary>
        /// <param param name="IDlog">Ma Loi</param>
        /// <param param name="ParaLog">Tham so di vao; Neu khong co truyen vao la NULL</param> 
        public void AppLog(string IDlog, string[] ParaLog)
        {
            WriteLog(IDlog, ParaLog);
        }
        /// <summary>
        /// Xoa het noi dung log trong thu muc log cua ung dung
        /// </summary>        
        public void ClearAllLog()
        {
            DeleteLog();
        }
        /// <summary>
        /// Xoa mot file noi dung log trong thu muc log cua ung dung
        /// </summary>   
        /// <param name="strFileName">Ten file log can xoa</param>
        public void ClearLog(string strFileName)
        {
            DeleteLog(strFileName);
        }
    }
}
